package com.pet.shop.service.pet;

import com.github.pagehelper.Page;
import com.pet.exceptions.BusinessException;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.shop.NoRelayDate;
import com.pet.shop.entity.shop.PetRelay;
import com.pet.shop.mappers.room.NoRelayDateMapper;
import com.pet.shop.mappers.shop.PetRelayMapper;
import com.pet.shop.service.option.IDataOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PetRelayServiceImpl implements IPetRelayService {

    @Autowired
    private PetRelayMapper petRelayMapper;
    @Autowired
    private NoRelayDateMapper noRelayDateMapper;
    @Autowired
    private IDataOptionService dataOptionService;

    @Override
    public int deleteEntity(Long id) {
        return petRelayMapper.delete(id);
    }

    @Override
    public int deleteEntityList(List<Long> idList) {
        if(idList == null || idList.isEmpty()) return 0;
        int i = 0;
        for (Long id : idList) {
            i += petRelayMapper.delete(id);
        }
        return i;
    }

    @Override
    public PetRelay insertEntity(PetRelay record) {
        petRelayMapper.insert(record);
        return record;
    }

    @Override
    public PetRelay getEntity(Long id) {
        return petRelayMapper.get(id);
    }

    @Override
    public int updateEntity(PetRelay record) {
        return petRelayMapper.update(record);
    }

    @Override
    public PageDto<PetRelay> queryPage(PageDto<PetRelay> pageDto) {
        Page<PetRelay> page = petRelayMapper.queryPage(pageDto);
        pageDto.setPageResult(page);
        return pageDto;
    }

    @Override
    public List<NoRelayDate> queryOrderByDate(Integer topNum) {
        return noRelayDateMapper.queryOrderByDate(topNum);
    }

    @Override
    public NoRelayDate insertNoRelayDate(NoRelayDate noRelayDate) {
        if(noRelayDate == null){
            throw new BusinessException("不可预约日期不能为空");
        }
        if(noRelayDate.getRelayDate() == null){
            throw new BusinessException("不可预约日期不能为空");
        }
        NoRelayDate serverDate = noRelayDateMapper.queryByRelayDate(noRelayDate.getRelayDate());
        if(serverDate != null){
            throw new BusinessException("不可预约日期已经存在");
        }

        noRelayDate.setCreateTime(new Date());
        noRelayDateMapper.insert(noRelayDate);
        return noRelayDate;
    }

    @Override
    public NoRelayDate queryByNoRelayDate(Date relayDate) {
        return noRelayDateMapper.queryByRelayDate(relayDate);
    }

    @Override
    public Boolean checkRelayDate(Date relayDate) {
        String[] weekDays = {"sundayRelay", "mondayRelay", "tuesdayRelay", "wednesdayRelay", "thursdayRelay", "fridayRelay", "saturdayRelay"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(relayDate);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        Map<String, String> optionMap = dataOptionService.queryDataOptionValMapByTypeId(-1L, 2);
        String val = optionMap.get(weekDays[w]);
        if("0".equals(val)) return false;
        NoRelayDate relayDate1 = queryByNoRelayDate(relayDate);
        return relayDate1 == null;
    }

    @Override
    public void deleteNoRelayDate(Long id) {
        noRelayDateMapper.delete(id);
    }
}
